﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Globalization;
using System.Web.Security;

namespace TagThisOrgProject.Models
{
    public class TagContext : DbContext
    {
        public TagContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<UserReport> UserReports { get; set; }
        public DbSet<UserBookmark> UserBookmarks { get; set; }
        public DbSet<Url> Url { get; set; }
        public DbSet<Tag> Tag { get; set; }
        public DbSet<Bookmark> Bookmarks { get; set; }
        public DbSet<UserUrlSettings> UserUrlSettings { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //здесь будем вызывать FluentApi

            modelBuilder.Entity<UserReport>().HasKey(r => r.UserReportId).Property(r => r.UserReportId).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<UserReport>().Property(ur => ur.Message).IsRequired().IsUnicode();
            modelBuilder.Entity<UserReport>().Property(ur => ur.Title).IsOptional().IsUnicode();
            modelBuilder.Entity<UserReport>().Property(ur => ur.Contacts).IsOptional().IsUnicode();
            modelBuilder.Entity<UserReport>().Property(ur => ur.UserId).IsRequired();

            modelBuilder.Entity<UserBookmark>().HasKey(ub => ub.UserBookmarkId).Property(ub => ub.UserBookmarkId).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<UserBookmark>().Property(ub => ub.UserId).IsRequired();
            modelBuilder.Entity<UserBookmark>().Property(ub => ub.BookmarkId).IsRequired();

            modelBuilder.Entity<Bookmark>().HasKey(b => b.BookmarkId).Property(b => b.BookmarkId).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<Bookmark>().Property(b => b.TagId).IsRequired();
            modelBuilder.Entity<Bookmark>().Property(b => b.UrlId).IsRequired();

            modelBuilder.Entity<Tag>().HasKey(t => t.TagId).Property(t => t.TagId).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<Tag>().Property(t => t.Text).IsRequired();

            modelBuilder.Entity<Url>().HasKey(u => u.UrlId).Property(u => u.UrlId).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<Url>().Property(u => u.Link).IsRequired().IsUnicode();
            modelBuilder.Entity<Url>().Property(u => u.Title).IsOptional();

            modelBuilder.Entity<UserUrlSettings>().HasKey(uus => uus.UserUrlSettingsId).Property(uus => uus.UserUrlSettingsId).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<UserUrlSettings>().Property(uus => uus.Title).IsRequired();
            modelBuilder.Entity<UserUrlSettings>().Property(uus => uus.UserId).IsRequired();
            modelBuilder.Entity<UserUrlSettings>().Property(uus => uus.UrlId).IsRequired();

            modelBuilder.Entity<UserProfile>().ToTable("UserProfile");
            modelBuilder.Entity<UserProfile>().HasKey(u => u.UserId).Property(u => u.UserId).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<UserProfile>().Property(u => u.UserName).IsRequired();



            modelBuilder.Entity<UserProfile>()
                .HasMany(r => r.Reports)
                .WithRequired(r => r.User)
                .HasForeignKey(r => r.UserId)
                .WillCascadeOnDelete(false);


            //Tag - * Bookmark
            modelBuilder.Entity<Tag>()
                .HasMany(t => t.Bookmarks)
                .WithRequired(b => b.Tag)
                .HasForeignKey(b => b.TagId)
                .WillCascadeOnDelete(false);

            //Url 1 - * Bookmark
            modelBuilder.Entity<Url>()
                .HasMany(u => u.Bookmarks)
                .WithRequired(b => b.Url)
                .HasForeignKey(b => b.UrlId)
                .WillCascadeOnDelete(false);

            //Bookmark 1 - * UserBookmarks
            modelBuilder.Entity<Bookmark>()
                .HasMany(b => b.UserBookmarks)
                .WithRequired(ub => ub.Bookmark)
                .HasForeignKey(ub => ub.BookmarkId)
                .WillCascadeOnDelete(false);

            //UserProfile 1 - * UserBookmarks
            modelBuilder.Entity<UserProfile>()
                .HasMany(u => u.UserBookmarks)
                .WithRequired(ub => ub.User)
                .HasForeignKey(ub => ub.UserId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<UserProfile>()
                .HasMany(u => u.UrlSettings)
                .WithRequired(s => s.User)
                .HasForeignKey(s => s.UserId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Url>()
                .HasMany(u => u.UserUrlSettings)
                .WithRequired(us => us.Url)
                .HasForeignKey(us => us.UrlId)
                .WillCascadeOnDelete(false);
        }
    }

    public class UserUrlSettings
    {
        public Int64 UserUrlSettingsId { get; set; }
        public int UserId { get; set; }
        public Int64 UrlId { get; set; }
        public string Title { get; set; }
        public virtual UserProfile User { get; set; }
        public virtual Url Url { get; set; }
    }

    public class UserBookmark
    {
        public Int64 UserBookmarkId { get; set; }
        public Int64 BookmarkId { get; set; }
        public int UserId { get; set; }
        public virtual Bookmark Bookmark { get; set; }
        public virtual UserProfile User { get; set; }
    }

    public class Bookmark
    {
        public Int64 BookmarkId { get; set; }
        public Int64 TagId { get; set; }
        public Int64 UrlId { get; set; }
        public bool isPrivate { get; set; }
        public virtual ICollection<UserBookmark> UserBookmarks { get; set; }
        public virtual Tag Tag { get; set; }
        public virtual Url Url { get; set; }
        public int position { get; set; }

    }
    public class Url
    {
        public Int64 UrlId { get; set; }
        public string Link { get; set; }
        public string HostUrl { get; set; }
        public string Content { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string PrintScreenUrl { get; set; }
        public string Favicon { get; set; }
        public DateTime LastModified { get; set; }
        public int position { get; set; }
        public bool isHiden { get; set; }
        public bool isFile { get; set; }
        public bool isSecure { get; set; }
        public virtual ICollection<Bookmark> Bookmarks { get; set; }
        public virtual ICollection<UserUrlSettings> UserUrlSettings { get; set; }
    }

    public class Tag
    {
        public Int64 TagId { get; set; }
        public string Text { get; set; }
        public virtual ICollection<Bookmark> Bookmarks { get; set; }
        public int position { get; set; }
    }

    public class UserProfile
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public virtual ICollection<UserReport> Reports { get; set; }
        public virtual ICollection<UserBookmark> UserBookmarks { get; set; }
        public virtual ICollection<UserUrlSettings> UrlSettings { get; set; }
    }

    public class UserReport
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int UserReportId { get; set; }
        public int UserId { get; set; }
        public string Message { get; set; }
        public string Title { get; set; }
        public string Contacts { get; set; }
        public virtual UserProfile User { get; set; }
    }





}
